package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.web.controller.DepartmentController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;


    @Transactional
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Transactional
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public void update(Permission departmen) {
        permissionMapper.updateByPrimaryKey(departmen);
    }

    @Transactional
    public Permission get(Long id) {

        return permissionMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Transactional
    public PageInfo<Permission> query(QueryObject qo) {
        //告诉Mybatis分页插件 设置查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<Permission>(permissions);


}

    @Autowired
    private ApplicationContext ctx;



    @Override
    public void reload() {
        //获取所有控制器处理方法
        /*不通用，永远改不完
        *  Method[] method1 = DepartmentController.class.getDeclaredMethods();
           Method[] method2 = EmployeeController.class.getDeclaredMethods();
           Method[] method3 = RoleController.class.getDeclaredMethods();
           Method[] method4 = XxxController.class.getDeclaredMethods();

        * */

        // 获取数据库所有的权限表达式查询出来
        List<Permission> permissions = permissionMapper.selectAll();
        Set<String> expressions = new HashSet<>();
        for(Permission permission : permissions) {
            expressions.add(permission.getExpression());
        }
        //获取所有控制器处理方法
        //通过Spring容器，获取所有控制器对象
        // 把所有的 Controller 中贴了权限注解的方法，转换成权限对象，并保存到数据库中
        // 利用 Spring上下文, 获取贴有 @Controller 注解的所有 bean
        Map<String, Object> beans =
                ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = beans.values();
      //遍历每个 Controller 对象，获取字节码对象，再获取其中所有的方法
        for (Object controller : controllers) {
                // 字节码对象
                Method[] methods = controller.getClass().getDeclaredMethods(); // 该字节码中所有的方法
                // 遍历每个方法
                for (Method method : methods) {
            // 判断方法上是否贴有我们的自定义的权限注解
                RequiredPermission annotation =
                        method.getAnnotation(RequiredPermission.class);
            // 判断注解是否为空
                if(annotation != null){
               //获取所有方法上的注解name和expression值
              // 获取注解中的权限名称 name
                    String name = annotation.name();
                // 获取注解中的权限表达式
                    String expression = annotation.expression();
                // 或者通过反射获取权限表达式
                 //String simpleName =
                  //  controller.getClass().getSimpleName().replace("Controller","");
                // uncapitalize方法可以把首字母变为小写
                 //String expression =
                 //   StringUtils.uncapitalize(simpleName)+":"+method.getName();
                // 如果没有存在数据库，就保存

                    //创建Permission对象，封装上面的值，调用permissionMapper保存的方法
                    if(!expressions.contains(expression)) {
                        // 封装成权限对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        // 把权限对象保存到数据库
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> queryByEmployeeId(Long employeeId) {

        List<String> strings = permissionMapper.selectByEmployeeId(employeeId);


        return strings;


    }


}


